package com.czy.utilslib.activity;



import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewbinding.ViewBinding;
import com.czy.utilslib.BaseApplication;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * 解决ViewBinding重复代码    （通过反射实现）
 * @param <viewBinding>     视图绑定类型
 */
public class BaseActivity<viewBinding extends ViewBinding> extends AppCompatActivity {
    protected viewBinding binding;
    protected String activityName;
    protected String TAG;

    public BaseActivity(String activityName){
        this.activityName = activityName;
        TAG = activityName;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViewBinding();

        init();

        setListener();
    }

    private void initViewBinding(){
        // 反射获取泛型的父类型即：BaseViewBindActivity<viewBinding extends ViewBinding>    （BaseViewBindActivity<ActivityMainBinding>）
        Type superclass = getClass().getGenericSuperclass();
        // 获取参数类型也就是 BaseViewBindActivity<ActivityMainBinding> -> ActivityMainBinding
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];

        try {
            // 反射获取 ActivityMainBinding.inflate方法；inflate是方法名称，LayoutInflater.class是方法传入参数
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            // 填充Binding
            binding = (viewBinding) method.invoke(null, getLayoutInflater());
            assert binding != null;
            setContentView(binding.getRoot());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void initWindow(){

/*        // 状态栏透明
        StatusBarUtil.setTranslucentStatusBar(this);

        // 隐藏顶部栏
        StatusBarUtil.setHideStatusBar(this);*/

        EdgeToEdge.enable(this);

        ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot(), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    // 将头部的Bar隐藏

    protected void init(){
        BaseApplication.getInstance().activityList.add(activityName);
        BaseApplication.getInstance().addActivity(activityName, this);
    }

    protected void setListener(){

    }

    @Override
    protected void onResume() {
        super.onResume();
        initWindow();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().activityList.remove(activityName);
        BaseApplication.getInstance().clearActivityInMap(activityName);
    }
}
